package ug.co.icom.kyazze.healthticket;

import java.util.List;

import ug.co.icom.kyazze.healthticket.DataBase.Database;
import ug.co.icom.kyazze.healthticket.data.Allergy;
import ug.co.icom.kyazze.healthticket.data.CustomListAllergyNames;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;


public class AllergyListNames extends Activity {
	ListView list;
	Database query_handler;
	@SuppressWarnings("unused")
	private ActionBarDrawerToggle mDrawerToggle;
	private int cFrag = R.string.main;
	
	//static ArrayList<String> outputx = new ArrayList<String>();

	Integer[] imageId = { R.drawable.ic_launcher, R.drawable.ic_launcher,
			R.drawable.ic_launcher, R.drawable.ic_launcher,
			R.drawable.ic_launcher };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.item_view2);
		
		// get the action bar
		ActionBar actionBar = getActionBar();

		// Enabling Back navigation on Action Bar icon
		actionBar.setDisplayHomeAsUpEnabled(true);
		
		SQLiteDatabase db = openOrCreateDatabase("mrDB", MODE_PRIVATE, null);
		query_handler = new Database(db);

		
		List<Allergy> all_objects = query_handler.getAllAllergies();
		String[] outputx = new String[all_objects.size()];
		
		int i = 0;

		for (Allergy aobj : all_objects) {

			String allergy_name1 = aobj.getAllergy_name();

			outputx[i] = allergy_name1;
			i++;
			//outputx.add(allergy_name1);

		}
		


		//final String[] web = outputx;

		CustomListAllergyNames adapter = new CustomListAllergyNames(AllergyListNames.this,outputx);
		list = (ListView) findViewById(R.id.content_frame2);
		list.setAdapter(adapter);
		list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// Toast.makeText(ImageList.this, "You Clicked at " +web[+
				// position], Toast.LENGTH_SHORT).show();
				
				switch (position) {
				
				case 0:
					//Pass the list value as a parameter to a new activity which queries the database
					String clicked_value = (String) parent.getItemAtPosition(position);
					Intent i = new Intent(getApplicationContext(), GenericAllergyViewer.class);
					i.putExtra("Allergy_Name", clicked_value);
					startActivity(i); 
					
					break;
					/*
					 * case 1: Intent b = new Intent(getApplicationContext(),
					 * UsersCappedToday.class); startActivity(b); break; case 2:
					 * Intent c = new Intent(getApplicationContext(),
					 * DataUsedToday.class); startActivity(c); break; case 4: //
					 * Intent e = new Intent(getApplicationContext(), //
					 * DataUsedToday.class); // startActivity(e); // break; case
					 * 5: Intent f = new Intent(getApplicationContext(),
					 * EnterParametersDataUsage.class); startActivity(f);
					 */
					//break;
				default:
					String clicked_value2 = (String) parent.getItemAtPosition(position);
					Intent i2 = new Intent(getApplicationContext(), GenericAllergyViewer.class);
					i2.putExtra("Allergy_Name", clicked_value2);
					startActivity(i2);

				}

			}
		});
		


				// enable ActionBar app icon to behave as action to toggle nav drawer
				getActionBar().setDisplayHomeAsUpEnabled(true);
				getActionBar().setDisplayShowHomeEnabled(true);
				getActionBar().setDisplayShowTitleEnabled(false);


					

	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainactionbar_menu, menu);

		// Associate searchable configuration with the SearchView
		SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
		SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
				.getActionView();
		searchView.setSearchableInfo(searchManager
				.getSearchableInfo(getComponentName()));
		return super.onCreateOptionsMenu(menu);
	}

	/* Called whenever we call invalidateOptionsMenu() */
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// If the nav drawer is open, hide action items related to the content
		// view
		// boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
		// menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
		return super.onPrepareOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// The action bar home/up action should open or close the drawer.
		// ActionBarDrawerToggle will take care of this.

		// Handle action buttons
		switch (item.getItemId()) {
		case R.id.action_search:
			return true;
		case R.id.action_notifications:
			loadNotifications();
			return true;
		case R.id.action_connections:
			loadConnections();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void loadNotifications() {
		if (cFrag != R.string.notifications) {
			cFrag = R.string.notifications;
			Fragment fragment = new NotificationsFragment();
			FragmentManager fragmentManager = getFragmentManager();
			fragmentManager.beginTransaction()
					.replace(R.id.content_frame2, fragment).commit();
		} else {
			cFrag = R.string.main;
			Fragment fragment = new MainItemsFragment();
			FragmentManager fragmentManager = getFragmentManager();
			fragmentManager.beginTransaction()
					.replace(R.id.content_frame2, fragment).commit();
		}
	}

	private void loadConnections() {
		if (cFrag != R.string.connections) {
			cFrag = R.string.connections;
			Fragment fragment = new ConnectionsFragment();
			FragmentManager fragmentManager = getFragmentManager();
			fragmentManager.beginTransaction()
					.replace(R.id.content_frame2, fragment).commit();
		} else {
			cFrag = R.string.main;
			Fragment fragment = new MainItemsFragment();
			FragmentManager fragmentManager = getFragmentManager();
			fragmentManager.beginTransaction()
					.replace(R.id.content_frame2, fragment).commit();
		}
	}
	

	



}
